package com.starblink.preferenceconfig.choosecomplete.ui

import android.os.Build
import android.os.Bundle
import android.os.CountDownTimer
import android.view.ViewGroup
import androidx.core.view.postDelayed
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.navigationTo
import com.starblink.basic.log.SKLogger
import com.starblink.basic.route.RoutePage
import com.starblink.preferenceconfig.bean.PreferenceBrand
import com.starblink.preferenceconfig.databinding.ActivityChooseComplete1Binding
import eightbitlab.com.blurview.BlurAlgorithm
import eightbitlab.com.blurview.RenderEffectBlur
import eightbitlab.com.blurview.RenderScriptBlur

/**
 * 选择完成，显示进度条
 */
@Route(path = RoutePage.PreferenceConfig.CHOOSE_COMPLETE)
class ChooseCompleteActivity : BaseTVMActivity<ActivityChooseComplete1Binding, ChooseCompleteVM>() {
    @Autowired(name = RoutePage.PreferenceConfig.ARGS_SELECTED_BRANDS)
    @JvmField
    var preferenceBrands: List<PreferenceBrand>? = null

    var countDownTimer: CountDownTimer? = null

    override fun initViewBinding(): ActivityChooseComplete1Binding =
        ActivityChooseComplete1Binding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        SKLogger.e("dss", "preferenceBrands=" + preferenceBrands)

        if (preferenceBrands.isNullOrEmpty()) {
            // skip进入
            viewBinding.layoutLogos.gone()
            viewBinding.vTextBig.text = "\uD83E\uDD14\nGuessing What You Like"
            viewBinding.vTextSmall.text = "You Can Also Manage Your Follows Later."
        } else {
            // 选择了品牌
            preferenceBrands?.let {
                if (it.size >= 1) {
                    viewBinding.logo1.visible()
                    viewBinding.logo1.load(it[0].image, it[0].name)
                }
                if (it.size >= 2) {
                    viewBinding.logo2.visible()
                    viewBinding.logo2.load(it[1].image, it[1].name)
                }
                if (it.size >= 3) {
                    viewBinding.logo3.visible()
                    viewBinding.logo3.load(it[2].image, it[2].name)
                }
                if (it.size >= 4) {
                    viewBinding.logo4.visible()
                    viewBinding.logo4.load(it[3].image, it[3].name)
                }
                if (it.size >= 5) {
                    viewBinding.logo5.visible()
                    viewBinding.logo5.load(it[4].image, it[4].name)
                }
                if (it.size >= 6) {
                    viewBinding.logoMore.visible()
                }
            }
        }

        viewBinding.root.post {
            countDownTimer = object : CountDownTimer(2000, 2000) {
                override fun onTick(millisUntilFinished: Long) {}

                override fun onFinish() {
                    goToMainPage()
                }
            }.start()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        countDownTimer?.cancel()
    }

    private fun goToMainPage() {
        // 去除无用参数
        getIntent().extras?.remove(RoutePage.PreferenceConfig.ARGS_SELECTED_BRANDS)

        navigationTo(RoutePage.Main.MAIN_ACTIVITY) {
            with(getIntent().extras) // !!!将启动页的Bundle值（如果有）带入首页
        }
        viewBinding.root.postDelayed(100) {
            finish()
        }
    }
}